<?php

namespace App\V3\V3_2_6\Controllers;

use App\V3\V3_2_6\Models\OperationLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class OperationLogController extends BaseController
{
    public function logList(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
//            'remark' =>'required|string',//搜索
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        /* if($this->getMainUserId() !== 1) {
            return $this->fail('', '权限不足，无法查看');
        } */

        if($this->getUserTpye()){
            $condition['main_id'] = 0;
        }else{
            if($this->getMainUserId() == 0 || $this->getUserId()== $this->getMainUserId()){
            $condition['main_id'] = $this->getUserId();
            }else{
            $condition['main_id'] = $this->getMainUserId();
            }
        }
        //查询条件
//        $condition[]=['main_id','=',$this->getMainUserId()];
        $condition1 = [];
        if(isset($data['remark']) && !empty($data['remark'])){
            $conditon1[] = ['remark','like','%'.$data['remark'].'%'];
        }
        //执行sql
        $model = OperationLogModel::select('a.id','a.remark','a.data','a.ip','a.created_at','b.username')
            ->from('operation_log as a')
            ->leftJoin('admin as b', 'a.user_id', '=', 'b.id')
            ->where($condition)
            ->where($condition1)
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])
            ->toArray();
        foreach ($model['data'] as &$val){
            $val['data'] = json_decode($val['data'],true);
        }
        //响应
        return $this->success($model);
    }
}
